package com.frank.easyexcel.web;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.frank.easyexcel.entity.TArea;
import com.frank.easyexcel.respository.TAreaRepository;
import com.frank.easyexcel.serive.AreaService;
import com.frank.easyexcel.serive.AreaTask;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.IteratorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author 小石潭记
 * @date 2021/7/5 10:12
 * @Description: ${todo 该demo失败了
 */
@RestController
@RequestMapping("/area")
@Slf4j
public class TAreaController {

    @Autowired
    private TAreaRepository tAreaRepository;

    @Autowired
    private AreaService areaService;

    @GetMapping("save")
    public void save() {
        long start = System.currentTimeMillis();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20,
                // 这里是设置非核心员工的“合同年限”（给线程续命的~~）
                0L,
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(100),
                new ThreadPoolExecutor.CallerRunsPolicy());
        for (int i = 0; i < 1000000; i++) {
            threadPoolExecutor.submit(new AreaTask(i, tAreaRepository));
        }
        System.out.println("时间：" + (System.currentTimeMillis() - start));
    }

    @GetMapping("/export")
    private void export() {
        try {
            String filePath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "export";
            log.info("-------", filePath);
            Iterable<TArea> all = tAreaRepository.findAll();
            List<TArea> list = new ArrayList<>();
            all.forEach(single-> list.add(single));
            ExcelWriter excelWriter = EasyExcel.write(filePath, TArea.class).build();
            WriteSheet sheet = EasyExcel.writerSheet("test").build();
            excelWriter.write(list, sheet);
            excelWriter.finish();
        } catch (Exception e) {
            System.out.println(e);
        }

    }

}
